package com.renrenyou.duanshipin.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.renrenyou.duanshipin.R;
import com.renrenyou.duanshipin.base.BaseActivity;
import com.renrenyou.duanshipin.bean.LoginData;
import com.renrenyou.duanshipin.databinding.ActivityLoginBinding;
import com.renrenyou.duanshipin.net.Constance;
import com.renrenyou.duanshipin.net.MyOkhttp;
import com.renrenyou.duanshipin.net.MyOkhttpCallback;
import com.renrenyou.duanshipin.util.ActivityUtil;
import com.renrenyou.duanshipin.util.ProgressDialogUtil;
import com.renrenyou.duanshipin.util.SharedPref;
import com.renrenyou.duanshipin.util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class LoginActivity extends BaseActivity<ActivityLoginBinding> implements View.OnClickListener {

    private AlertDialog mDialog;
    private Call mCall;

    @Override
    public ActivityLoginBinding getBinding(LayoutInflater inflater) {
        return ActivityLoginBinding.inflate(inflater);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding.tvLoginLogin.setOnClickListener(this);

        mBinding.tbvLogin.getSubTitle().setOnClickListener(view -> {
            ActivityUtil.startActivity(this, RegisterActivity.class);
        });
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_login_login) {
            Editable phoneTable = mBinding.edtLoginPhone.getText();
            Editable pwdTable = mBinding.edtLoginPwd.getText();
            if (phoneTable != null && pwdTable != null) {
                String phone = phoneTable.toString().trim();
                String pwd = pwdTable.toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showToast(this, "请输入手机号");
                    return;
                }
                if (TextUtils.isEmpty(pwd)) {
                    ToastUtil.showToast(this, "请输入密码");
                    return;
                }
                login(phone, pwd);
            }
        }
    }

    private void login(String phone, String pwd) {
        Map<String, Object> params = new HashMap<>();
        params.put("appCode", "com.android.sucai2025");
        params.put("versionNo", "100");
        params.put("channel", "android");
        params.put("userName", phone);
        params.put("password", pwd);
        mDialog = ProgressDialogUtil.showProgressDialog(this);
        mCall = MyOkhttp.getInstance().post(Constance.BASE_URL + Constance.LOGIN, params, new MyOkhttpCallback<LoginData>() {
            @Override
            public void onFailed(int errorCode, int errorMsg) {
                mDialog.dismiss();
                ToastUtil.showToast(LoginActivity.this, getString(errorMsg));
            }

            @Override
            public void onSuccess(LoginData s) {
                mDialog.dismiss();
                if (s != null) {
                    if (s.isSuccess()) {
                        Constance.isLogin = true;
                        LoginData.LogInfo logInfo = s.getData();
                        if (logInfo != null) {
                            Constance.TOKEN = logInfo.getToken();
                            SharedPref.getInstance().putString("token", logInfo.getToken());
                            SharedPref.getInstance().putString("phone", phone);
                        }
                        loginSuccess(phone);
                    } else {
                        ToastUtil.showToast(LoginActivity.this, s.getMessage());
                    }
                } else {
                    ToastUtil.showToast(LoginActivity.this, "登录失败");
                }
            }
        });
    }

    private void loginSuccess(String phone) {
        Intent intent = new Intent("loginSuc");
        intent.putExtra("phone", phone);
        sendBroadcast(intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        if (mCall != null) {
            mCall.cancel();
        }
        super.onDestroy();
    }
}
